<template>
 <div>
  <a-layout style="padding: 24px 0; background: #fff">
  <a-layout-sider width="200" style="background: #fff">
    <a-menu
      mode="inline"
      style="height: 100%"
      @click="handleClick"
    >
    <a-menu-item key="welcome">
            <MailOutlined />
            <span>欢迎</span>
          </a-menu-item>
          <a-sub-menu v-for="item in level1" :key="item.id">
            <template v-slot:title>
              <span><user-outlined />{{item.name}}</span>
            </template>
            <a-menu-item v-for="child in item.children" :key="child.id">
              <MailOutlined /><span>{{child.name}}</span>
            </a-menu-item>
          </a-sub-menu>
        </a-menu>
  </a-layout-sider>
  <a-layout-content :style="{ padding: '0 24px', minHeight: '280px' }">
    <div class="welcome" v-show="isShowWelcome">
      <TheWelcome/>
        </div>
    <a-list v-show="!isShowWelcome" item-layout="vertical" size="large" :grid="{ gutter: 20, column: 3 }" :data-source="ebooks">
    <template #renderItem="{ item }">
      <a-list-item key="item.name">
        <!-- <template #actions>
          <span v-for="{ icon, text } in actions" :key="icon">
            <component :is="icon" style="margin-right: 8px" />
            {{ text }}
          </span>
        </template> -->
        <template #actions>
          <span >
            <component :is="'FileOutlined'" style="margin-right: 8px" />
            {{ item.docCount }}
          </span>
                <span >
            <component :is="'UserOutlined'" style="margin-right: 8px" />
            {{ item.viewCount }}
          </span>
          <span >
            <component :is="'LikeOutlined'" style="margin-right: 8px" />
            {{ item.voteCount }}
          </span>
        </template>   

        <a-list-item-meta :description="item.description">
          <template #title>
            <router-link :to="'/doc?ebookId='+item.id">
                        {{item.name}}
                    </router-link>
          </template>
          <template #avatar><a-avatar :src="item.cover" /></template>
        </a-list-item-meta>
      </a-list-item>
    </template>
  </a-list>
  </a-layout-content>
</a-layout>
  </div>
</template>
<script lang="ts" setup>
import { ref,defineComponent,onMounted } from 'vue';
import TheWelcome from '@/components/WelcomeView.vue';
import {Tool} from '@/utils/tool.ts';
import axios from 'axios';
import {message} from 'ant-design-vue';
import { UserOutlined, LaptopOutlined, NotificationOutlined } from '@ant-design/icons-vue';
import { StarOutlined, LikeOutlined, MessageOutlined } from '@ant-design/icons-vue';
const ebooks = ref([]);
onMounted(()=>{
  handleQueryEbook();
});
const handleQueryEbook = ()=>{
      axios.get("http://localhost:8080/obwiki/ebook/list",{
        params:{
          page:1,
          size:1000,
          categoryId2:categoryId2
        }
      }).then((resp)=>{
        ebooks.value = resp.data.content.list;
      })
    }
    const isShowWelcome = ref(true);
    let categoryId2 = 0;
	//点击分类导航栏 获取选中id,发送查询请求
    const handleClick = (value:any)=>{
      if (value.key === 'welcome'){
        isShowWelcome.value = true;
      } else {
        categoryId2 = value.key;
        isShowWelcome.value = false;
        handleQueryEbook();
      }
    } 

const actions: Record<string, any>[] = [
  { icon: StarOutlined, text: '156' },
  { icon: LikeOutlined, text: '156' },
  { icon: MessageOutlined, text: '2' },
];
onMounted(()=>{
  handleQueryCategory();
})
    
 /*
  * 分类相关
  * */
  const level1 = ref();
    const  handleQueryCategory = ()=>{
      axios.get("http://localhost:8080/obwiki/category/all").then((resp)=>{
        const  data = resp.data;

        if (data.success){
          console.log("原始数组",data.content);

          level1.value = [];
          level1.value = Tool.array2Tree(data.content,0);
          console.log("树形结构：",level1);
        } else {
          message.error(data.message);
        }
      });
    };

</script>
<style scoped>
 .logo {
  float: left;
  width: 120px;
  height: 31px;
  margin: 16px 24px 16px 0;
  background: rgba(255, 255, 255, 0.3);
}

.ant-row-rtl #components-layout-demo-top-side .logo {
  float: right;
  margin: 16px 0 16px 24px;
}

.site-layout-background {
  background: #fff;
}
</style>
